
import java.io.IOException;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Andik Taufiq
 */
public class Settings {
    private Font plainFont;
    private Font boldFont;
    private Font mediumPlainFont;
    private Font mediumBoldFont;
    public int[] finalState;
    public int stateChooser;
    /*
     * 1 --> [ 0 1 2 3 4 5 6 7 8 ]
     * 2 --> [ 1 2 3 4 5 6 7 8 0 ]
     * 3 --> [ 1 2 3 8 0 4 7 6 5 ]
     */
    private int NB_ELEMENT = 9;
    public String solver;
    public int solverChooser;
    /*
     * solver describes the algorithm which used for solving the problem
     * 1 --> Hill Climbing
     * 2 --> Optimized Hill Climbing
     * 3 --> A*
     * 4 --> Optimized A*
     */
    public long delay = 150;
    public int step = 20;

    public SettingsMenu sm1;
    public SettingsMenu sm2;
    public SettingsMenu sm3;
    public SettingsMenu sm4;

    public Image highlight;
    public int index = 0;

    public int xRef, yRef;
    public int deltaY = 0;

    public Image arrowLeft;
    public Image arrowRight;

    public Settings() {
        // Set the default solver
        solverChooser = 1;
        // Set the default state chooser
        stateChooser = 1;
        // Initialize everything
        initialize();
        setMenus();
    }

    private void initialize() {
        switch (solverChooser) {
            case 1:
                solver = "Hill Climbing";
            break;
            case 2:
                solver = "Optimized Hill Climbing";
            break;
            case 3:
                solver = "Different Hill Climbing";
            break;
            case 4:
                solver = "Opt. Diff. Hill Climbing";
            break;
            case 5:
                solver = "Best First";
            break;
            case 6:
                solver = "A*";
            break;
        }
        finalState = new int[NB_ELEMENT];
        switch (stateChooser) {
            case 1:
                finalState[0] = 0;
                finalState[1] = 1;
                finalState[2] = 2;
                finalState[3] = 3;
                finalState[4] = 4;
                finalState[5] = 5;
                finalState[6] = 6;
                finalState[7] = 7;
                finalState[8] = 8;
            break;
            case 2:
                finalState[0] = 1;
                finalState[1] = 2;
                finalState[2] = 3;
                finalState[3] = 4;
                finalState[4] = 5;
                finalState[5] = 6;
                finalState[6] = 7;
                finalState[7] = 8;
                finalState[8] = 0;
            break;
            case 3:
                finalState[0] = 1;
                finalState[1] = 2;
                finalState[2] = 3;
                finalState[3] = 8;
                finalState[4] = 0;
                finalState[5] = 4;
                finalState[6] = 7;
                finalState[7] = 6;
                finalState[8] = 5;
            break;
        }
        try {
            highlight		= Image.createImage("/highlight2.png");
            arrowLeft		= Image.createImage("/tab_left.png");
            arrowRight		= Image.createImage("/tab_right.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        plainFont 	= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        boldFont	= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        mediumPlainFont	= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
        mediumBoldFont	= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);

        xRef = 10;
        yRef = 45;
    }

    public void setMenus() {
        sm1 = new SettingsMenu("Solver", 1, solverChooser, solver);
        sm2 = new SettingsMenu("Goal State", 2, stateChooser, strArray(finalState));
        sm3 = new SettingsMenu("Random Step", 3, 0, Integer.toString(step));
        sm4 = new SettingsMenu("Animation Speed", 4, 0, Double.toString(delay));
    }

    public void update() {
        switch (solverChooser) {
            case 1:
                solver = "Hill Climbing";
            break;
            case 2:
                solver = "Optimized Hill Climbing";
            break;
            case 3:
                solver = "Different Hill Climbing";
            break;
            case 4:
                solver = "Opt. Diff. Hill Climbing";
            break;
            case 5:
                solver = "Best First";
            break;
            case 6:
                solver = "A*";
            break;
        }
        switch (stateChooser) {
            case 1:
                finalState[0] = 0;
                finalState[1] = 1;
                finalState[2] = 2;
                finalState[3] = 3;
                finalState[4] = 4;
                finalState[5] = 5;
                finalState[6] = 6;
                finalState[7] = 7;
                finalState[8] = 8;
            break;
            case 2:
                finalState[0] = 1;
                finalState[1] = 2;
                finalState[2] = 3;
                finalState[3] = 4;
                finalState[4] = 5;
                finalState[5] = 6;
                finalState[6] = 7;
                finalState[7] = 8;
                finalState[8] = 0;
            break;
            case 3:
                finalState[0] = 1;
                finalState[1] = 2;
                finalState[2] = 3;
                finalState[3] = 8;
                finalState[4] = 0;
                finalState[5] = 4;
                finalState[6] = 7;
                finalState[7] = 6;
                finalState[8] = 5;
            break;
        }
    }

    public void paintMenu(Graphics g) {
        update();
        setMenus();
        deltaY = 0;
        g.drawImage(highlight, xRef - 5, yRef + (47 * index), Graphics.TOP | Graphics.LEFT);
        if (index == 0)
            g.setColor(0xFFF005);
        else
            g.setColor(0x8EBCE8);
        g.setFont(boldFont);
        g.drawString(sm1.label, xRef + 5, yRef + 5 + deltaY, Graphics.TOP | Graphics.LEFT);
        if (index == 0)
            g.setColor(0xDDDDDD);
        else
            g.setColor(0x4A77A2);
        g.setFont(plainFont);
        g.drawString(sm1.content, xRef + 40, yRef + 23 + deltaY, Graphics.TOP | Graphics.LEFT);
        g.drawImage(arrowLeft, xRef + 35, yRef + 23 + deltaY, Graphics.TOP | Graphics.RIGHT);
        g.drawImage(arrowRight, xRef + 45 + plainFont.stringWidth(sm1.content), yRef + 23 + deltaY, Graphics.TOP | Graphics.LEFT);

        deltaY = deltaY + 47;
        if (index == 1)
            g.setColor(0xFFF005);
        else
            g.setColor(0x8EBCE8);
        g.setFont(boldFont);
        g.drawString(sm2.label, xRef + 5, yRef + 5 + deltaY, Graphics.TOP | Graphics.LEFT);
        if (index == 1)
            g.setColor(0xDDDDDD);
        else
            g.setColor(0x4A77A2);
        g.setFont(plainFont);
        g.drawString(sm2.content, xRef + 40, yRef + 23 + deltaY, Graphics.TOP | Graphics.LEFT);
        g.drawImage(arrowLeft, xRef + 35, yRef + 23 + deltaY, Graphics.TOP | Graphics.RIGHT);
        g.drawImage(arrowRight, xRef + 45 + plainFont.stringWidth(sm2.content), yRef + 23 + deltaY, Graphics.TOP | Graphics.LEFT);

        deltaY = deltaY + 47;
        if (index == 2)
            g.setColor(0xFFF005);
        else
            g.setColor(0x8EBCE8);
        g.setFont(boldFont);
        g.drawString(sm3.label, xRef + 5, yRef + 5 + deltaY, Graphics.TOP | Graphics.LEFT);
        if (index == 2)
            g.setColor(0xDDDDDD);
        else
            g.setColor(0x4A77A2);
        g.setFont(plainFont);
        g.drawString(sm3.content, xRef + 40, yRef + 23 + deltaY, Graphics.TOP | Graphics.LEFT);
        g.drawImage(arrowLeft, xRef + 35, yRef + 23 + deltaY, Graphics.TOP | Graphics.RIGHT);
        g.drawImage(arrowRight, xRef + 45 + plainFont.stringWidth(sm3.content), yRef + 23 + deltaY, Graphics.TOP | Graphics.LEFT);

        deltaY = deltaY + 47;
        if (index == 3)
            g.setColor(0xFFF005);
        else
            g.setColor(0x8EBCE8);
        g.setFont(boldFont);
        g.drawString(sm4.label, xRef + 5, yRef + 5 + deltaY, Graphics.TOP | Graphics.LEFT);
        if (index == 3)
            g.setColor(0xDDDDDD);
        else
            g.setColor(0x4A77A2);
        g.setFont(plainFont);
        g.drawString(sm4.content, xRef + 40, yRef + 23 + deltaY, Graphics.TOP | Graphics.LEFT);
        g.drawImage(arrowLeft, xRef + 35, yRef + 23 + deltaY, Graphics.TOP | Graphics.RIGHT);
        g.drawImage(arrowRight, xRef + 45 + plainFont.stringWidth(sm4.content), yRef + 23 + deltaY, Graphics.TOP | Graphics.LEFT);
    }

    public String strArray(int[] array) {
        String temp = "[ ";
        for (int i=0; i < array.length; i++) {
            temp = temp + Integer.toString(array[i]) + " ";
        }
        temp = temp + "]";
        return temp;
    }

    public class SettingsMenu {
        public String label;
        public int type;
        /*
         * 1 --> solver, which algorithm we use
         * 2 --> final state, there are 3 options
         * 3 --> random step (1 - 50)
         * 4 --> animated speed (50 - 1000) in millisecond
         */
        public int index;
        public String content;
        public int x, y;

        public SettingsMenu(String label, int type, int index, String content) {
            this.label = label;
            this.type = type;
            this.index = index;
            this.content = content;
        }
    }
}
